﻿using StorageApi.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace StorageApi.Areas.Admin
{
    public class AdminAjaxController : ApiController
    {
        JooWMSEntities db = new JooWMSEntities();
        // GET api/<controller>
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        // GET api/<controller>/5
        public object Get(int id)
        {
            if (id == 1)
            {
                var adminlist = db.SysRole.ToList();
                return adminlist;
            }
            else if (id == 2)
            {
                var dep = db.SysDepart.ToList();
                return dep;
            }
            return "value";
        }
        [ActionName("FenYeApi")]
        public object Get(int id, int page, int size, string msg, [FromUri] StorageApi.Models.Admin admin)
        {


            string UserCode = admin.UserCode;
            string UserName = admin.UserName;
            string DepartNum = admin.DepartNum;
            string RoleNum = admin.RoleNum;

            if (UserCode == null)
            {
                UserCode = "";
            }
            if (UserName == null)
            {
                UserName = "";
            }

            if (msg == null)
            {
                msg = "";
            }
            if (id == 1)
            {
                var userlist = db.Admin.Where(a => a.UserCode.Contains(msg) || a.UserName.Contains(msg)).OrderBy(item => item.ID).Skip((page - 1) * size).Take(size).ToList();
                return userlist;
            }
            else if (id == 2)
            {
                int[] sai = new int[3];
                var count = db.Admin.Where(a => a.UserCode.Contains(msg) || a.UserName.Contains(msg)).OrderBy(item => item.ID).Count();

                sai[0] = (int)Math.Ceiling(count * 1.0 / size);
                sai[1] = count;
                sai[2] = page;

                return sai;
            }
            else if (id == 3)
            {
                var userlist = db.Admin.Where(a => a.UserCode.Contains(UserCode) && a.UserName.Contains(UserName)).OrderBy(item => item.ID).Skip((page - 1) * size).Take(size);
                if (DepartNum != "0")
                {
                    userlist = userlist.Where(a => a.DepartNum == DepartNum);
                }
                if (RoleNum != "0")
                {
                    userlist = userlist.Where(a => a.RoleNum == RoleNum);
                }
                var list = userlist.ToList();
                return userlist.ToList();
            }
            else if (id == 4)
            {
                int[] sai = new int[3];
                var userlist = db.Admin.Where(a => a.UserCode.Contains(UserCode) && a.UserName.Contains(UserName)).OrderBy(item => item.ID).Skip((page - 1) * size).Take(size);
                if (DepartNum != "0")
                {
                    userlist = userlist.Where(a => a.DepartNum == DepartNum);
                }
                if (RoleNum != "0")
                {
                    userlist = userlist.Where(a => a.RoleNum == RoleNum);
                }

                var count = userlist.Count();

                sai[0] = (int)Math.Ceiling(count * 1.0 / size);
                sai[1] = count;
                sai[2] = page;

                return sai;
            }
            return "niao";
        }

        // POST api/<controller>
        public string Post([FromBody] StorageApi.Models.Admin value)
        {
            //获取时间戳
            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            string ret = string.Empty;
            ret = Convert.ToInt64(ts.TotalSeconds).ToString() + DateTime.Now.Millisecond;

            value.UserCode = ret;
            value.CreateTime = DateTime.Now;
            value.LoginCount = 0;
            value.UpdateTime = DateTime.Now;
            value.IsDelete = 0;
            value.Status = 0;
            value.ParentCode = "";
            if (value.RealName == null)
            {
                value.RealName = "";
            }
            db.Admin.Add(value);
            if (db.SaveChanges() > 0)
            {
                return "0";
            }
            return "1";
        }

        // PUT api/<controller>/5
        public int Put(string id, [FromBody] StorageApi.Models.Admin value)
        {
            var arr = db.Admin.Where(a => a.UserCode == value.UserCode).FirstOrDefault();
            arr.UserName = value.UserName;
            arr.RealName = value.RealName;
            arr.Email = value.Email;
            arr.Mobile = value.Mobile;
            arr.Phone = value.Phone;
            arr.DepartNum = value.DepartNum;
            arr.RoleNum = value.RoleNum;
            if (value.PassWord==null)
            {
                value.PassWord = "";
            }
            arr.PassWord = value.PassWord;
            db.Entry(arr).State = System.Data.Entity.EntityState.Modified;
            int i = db.SaveChanges();
            return i;
        }

        // DELETE api/<controller>/5
        public int Delete(int id)
        {
            var list = db.Admin.Find(id);
            db.Admin.Remove(list);
            if (db.SaveChanges() > 0)
            {
                return 1;
            }
            return 0;
        }
    }
}